$(function () {
    var storage = window.localStorage;
    //重新请求服务器 更新
    setTimeout(function () {
        $.get('/getUserInfo', function (userInfo) {
            buildUserInfo(userInfo);
            userInfo = JSON.stringify(userInfo);
            storage.setItem("userInfo", userInfo);
        });
        $.get('/Sidebar', function (data) {
            buildSidebar(data);
            data = JSON.stringify(data);
            storage.setItem("sidebar", data);
        });
    }(), 500);

    function buildUserInfo(userInfo) {
        console.log(userInfo);
        if ([] !== userInfo && null != userInfo && 'undefined' != typeof(userInfo.nick)) {
            storage.setItem("userInfo", userInfo);
            $('#user_info').find('.am-btn').hide();
            $('#user_info').find('.am-topbar-brand .am-icon-user').html(' ' + userInfo.nick);
            $('#user_info').find('.am-topbar-brand').show();
        } else {
            $('#user_info').find('.am-topbar-brand').hide();
            $('#user_info').find('.am-topbar-brand .am-icon-user').html('');
            $('#user_info').find('.am-btn').show();
        }
    }

    function buildSidebar(data) {
        if ([] != data && null != data) {
            $('#sidebar-news').html('');
            $('#sidebar-scenery').html('');
            //新闻
            $.each(data.notice_list.data, function (key, val) {
                $('#sidebar-notice').html('<p>' + val.description + '</p>');
            });
            //新闻
            $.each(data.news_list.data, function (key, val) {
                $('#sidebar-news').append('<li><a href="' + val.url + '" target="_blank">' + val.title + '</a></li>');
            });
            //热门景点
            $.each(data.scenery_list.data, function (key, val) {
                $('#sidebar-scenery').append('<li><a href=/scenery/' + val.id + '.html>' + val.name + '<img class="am-thumbnail" src="' + val.pic_url + '" alt=""/></a></li>');
            });
            //热门搜索
            $.each(data.search_list.data, function (key, val) {
                keywords = val.keywords;
                $('#sidebar-search').append('<a class="am-btn am-btn-default am-btn-sm" style="margin:5px;" href="/search.html?keywords=' + keywords + '">' + keywords + '</a>');
            });

        }
    }
});